package controllers

import (
	"gitee.com/igolang/imoney/internal/app/system/services"
	"gitee.com/igolang/imoney/internal/common/httpResp"
	"github.com/gin-gonic/gin"
)

var User = &user{}

type user struct{}

func (c *user) List(ctx *gin.Context) {
	r := httpResp.Response()
	defer httpResp.JSON(ctx, r)
	data, err := services.User.List(ctx)
	if err != nil {
		httpResp.ErrorResponse(err, r)
		return
	}
	r.Data = data
}

func (c *user) Export(ctx *gin.Context) {
	r := httpResp.Response()
	b, err := services.User.Export(ctx)
	if err != nil {
		httpResp.ErrorResponse(err, r)
		httpResp.JSON(ctx, r)
		return
	}
	httpResp.Export(ctx, b, "xlsx")
}

func (c *user) RoleAndPost(ctx *gin.Context) {
	r := httpResp.Response()
	defer httpResp.JSON(ctx, r)
	data, err := services.User.RoleAndPost(0)
	if err != nil {
		httpResp.ErrorResponse(err, r)
		return
	}
	r.Data = data
}

func (c *user) DeptTree(ctx *gin.Context) {
	r := httpResp.Response()
	defer httpResp.JSON(ctx, r)
	data, err := services.User.DeptTree(ctx)
	if err != nil {
		httpResp.ErrorResponse(err, r)
		return
	}
	r.Data = data
}

func (c *user) Detail(ctx *gin.Context) {
	r := httpResp.Response()
	defer httpResp.JSON(ctx, r)
	data, err := services.User.Detail(ctx)
	if err != nil {
		httpResp.ErrorResponse(err, r)
		return
	}
	r.Data = data
}

func (c *user) Profile(ctx *gin.Context) {
	r := httpResp.Response()
	defer httpResp.JSON(ctx, r)
	data, err := services.User.Profile(ctx)
	if err != nil {
		httpResp.ErrorResponse(err, r)
		return
	}
	r.Data = data
}

func (c *user) ProfileAvatar(ctx *gin.Context) {
	r := httpResp.Response()
	defer httpResp.JSON(ctx, r)
	data, err := services.User.ProfileAvatar(ctx)
	if err != nil {
		httpResp.ErrorResponse(err, r)
		return
	}
	r.Data = gin.H{
		"imgUrl": data,
	}
}

func (c *user) SaveProfile(ctx *gin.Context) {
	r := httpResp.Response()
	defer httpResp.JSON(ctx, r)
	if err := services.User.SaveProfile(ctx); err != nil {
		httpResp.ErrorResponse(err, r)
		return
	}
}

func (c *user) ProfileUpdatePwd(ctx *gin.Context) {
	r := httpResp.Response()
	defer httpResp.JSON(ctx, r)
	if err := services.User.ProfileUpdatePwd(ctx); err != nil {
		httpResp.ErrorResponse(err, r)
		return
	}
}

func (c *user) Delete(ctx *gin.Context) {
	r := httpResp.Response()
	defer httpResp.JSON(ctx, r)
	if err := services.User.Delete(ctx); err != nil {
		httpResp.ErrorResponse(err, r)
		return
	}
}

func (c *user) Add(ctx *gin.Context) {
	r := httpResp.Response()
	defer httpResp.JSON(ctx, r)
	if err := services.User.Add(ctx); err != nil {
		httpResp.ErrorResponse(err, r)
		return
	}
}

func (c *user) Update(ctx *gin.Context) {
	r := httpResp.Response()
	defer httpResp.JSON(ctx, r)
	if err := services.User.Save(ctx); err != nil {
		httpResp.ErrorResponse(err, r)
		return
	}
}

func (c *user) ChangeStatus(ctx *gin.Context) {
	r := httpResp.Response()
	defer httpResp.JSON(ctx, r)
	if err := services.User.ChangeStatus(ctx); err != nil {
		httpResp.ErrorResponse(err, r)
		return
	}
}

func (c *user) ResetPwd(ctx *gin.Context) {
	r := httpResp.Response()
	defer httpResp.JSON(ctx, r)
	if err := services.User.ResetPwd(ctx); err != nil {
		httpResp.ErrorResponse(err, r)
		return
	}
}

func (c *user) AuthRoleList(ctx *gin.Context) {
	r := httpResp.Response()
	defer httpResp.JSON(ctx, r)
	data, err := services.User.AuthRoleList(ctx)
	if err != nil {
		httpResp.ErrorResponse(err, r)
		return
	}
	r.Data = data
}

func (c *user) AuthRole(ctx *gin.Context) {
	r := httpResp.Response()
	defer httpResp.JSON(ctx, r)
	if err := services.User.AuthRole(ctx); err != nil {
		httpResp.ErrorResponse(err, r)
		return
	}
}
